<template>
  <div>

    <q-layout>
      <q-layout-header ref="header">
        <xheader :title="title"></xheader>
      </q-layout-header>
      <q-page-container>

        <q-page padding>

          <q-input ref="code" float-label="随工单号" v-model="code" clearable
                   @blur="searchBatchInfo"
                   @keydown.enter="searchBatchInfo" :after="$parent.getScanIcon('code',this)"/>
          <q-input float-label="随工单信息" placeholder="请输入随工单号" readonly v-model="codeInfo" type="textarea" rows="4"
                   hide-underline/>
          <q-input ref="batchNo" float-label="坩埚条码" v-model="batchNo" clearable
                   @blur="getMaterialInfo" :after="$parent.getScanIcon('batchNo',this)"
                   @keydown.enter="getMaterialInfo"/>
          <q-input float-label="坩埚信息" placeholder="请输入坩埚条码" readonly v-model="batchInfo" type="textarea" rows="3"
                   hide-underline/>
          <q-btn label="提交" color="primary" class="full-width" @click="submit"/>
          <br><br>
          <q-table
            :data="data"
            :columns="columns"
            dense
            separator="cell"
            :pagination="{page:1,rowsPerPage:20}"
            hide-bottom
            row-key="name"
          >
            <q-td slot="body-cell-BATCH_NO" slot-scope="props" :props="props">
              <span :style="'color:' +getBatchNoColor(props.row.LONG_TYPE)">{{ props.value }}</span>
            </q-td>
          </q-table>
        </q-page>

      </q-page-container>
    </q-layout>
  </div>
</template>

<script>

import utils from '../../js/utils'
import Xheader from '../../components/xheader'

export default {
  name: 'use_ganguo',
  components: { Xheader },
  props: {
    title: {}
  },
  data: () => ({
    code: '',
    codeInfo: '',
    batchNo: '',
    batchInfo: '',
    batchId: '',
    ggBatchNo: '',
    sgdModel: '',
    ggModel: '',
    columns: [
      { name: 'BATCH_NO', label: '坩埚批次', field: 'BATCH_NO' },
      { name: 'NAME', label: '名称', field: 'NAME' },
      { name: 'NORM_STR', label: '规格型号', field: 'NORM_STR' },
      { name: 'STATUS', label: '喷涂状态', field: 'STATUS' },
      { name: 'IS_PASS', label: '是否合格', field: 'IS_PASS' },
      { name: 'FINISH_TIME', label: '完成喷涂时间', field: 'FINISH_TIME' },
      { name: 'FINISH_LONG', label: '时长/h', field: 'FINISH_LONG' },
      { name: 'DESCRIPTION', label: '备注', field: 'DESCRIPTION' }
    ],
    data: []

  }),
  computed: {},
  methods: {
    submit () {
      if (!this.ggBatchNo) {
        this.$parent.info('主材批次不能为空！')
        return
      }

      if (this.sgdModel !== '' && this.ggModel.match(sgdModel) === null && (this.sgdModel === 'G6' || this.sgdModel === 'G7')) {
        this.$parent.info('随工单所需坩埚和领用坩埚的规格不匹配！')
        return
      }
      this.axios.post(this.$conf.server + '/useMaterial/subGanguoGet', this.$qs.stringify({
        id: this.batchId, batchNo: this.ggBatchNo
      })).then(res => {
        this.$parent.info(res.data.msg)
        if (res.data.result) {
          this.ggBatchNo = ''
          this.batchId = ''
          this.code = ''
          this.batchNo = ''
          this.codeInfo = ''
          this.batchInfo = ''
        }
      })
    },
    popupFilter () {

    },
    /* scanReturn (data, name) {
        this[name] = data
        this.$refs[name].focus()
        this.$parent.setComponent(undefined, undefined)
      }, */
    searchBatchInfo () {
      if (this.code !== '') {
        this.axios.post(this.$conf.server + '/manuBatch/searchBatchInfo', this.$qs.stringify({
          code: this.code,
          codeType: 'A'
        })).then(res => {
          if (res.data.result) {
            this.batchId = res.data.ID
            this.sgdModel = res.data.CUT_METHOD
            this.codeInfo = '规格：' + utils.nvl(res.data.CUT_METHOD) +
                '\r\n产品名称：' + utils.nvl(res.data.PNAME) +
                '\r\n配方名称：' + utils.nvl(res.data.PFNAME) + '(' + utils.nvl(res.data.VERSION) + ')' +
                (res.data.IS_TEST === 'Y' ? '\r\n<span style=\'color: #f00;\'>试验件</span>' : '') +
                '\r\n' + utils.nvl(res.data.DESCRIPTION)
          } else {
            this.$parent.info(res.data.msg)
          }
        })
      }
    },
    getMaterialInfo () {
      if (this.batchNo !== '') {
        this.axios.post(this.$conf.server + '/useMaterial/getMaterialInfo',
          this.$qs.stringify({ batchNo: this.batchNo })).then(res => {
          if (res.data.result) {
            this.ggBatchNo = this.batchNo
            this.ggModel = res.data.NORM
            this.batchInfo = utils.nvl(res.data.PCODE) + '，' + utils.nvl(res.data.PNAME) +
                (res.data.VERDORCODE ? '\r\n供应商：' + utils.nvl(res.data.VERDORCODE) + '(' + utils.nvl(res.data.VERDORNAME) + ')' : '') +
                '\r\n' + utils.nvl(res.data.DESCRIPTION)
          } else {
            this.$parent.info(res.data.msg)
          }
        })
      }
    },
    getGanguoSprayHis (longType) {
      this.axios.post(this.$conf.server + '/ganguoCheck/getSprayHis', this.$qs.stringify({
        ggCode: '',
        longType: longType
      })).then(res => {
        if (res.data.result) {
          this.data = res.data.sprayHis.rows
        } else {
          this.$parent.info(res.data.msg)
        }
      })
    },
    getBatchNoColor (longType) {
      let color
      if (longType === 1) {
        color = 'green'
      } else if (longType === 0) {
        color = '#e0c714'
      } else if (longType === -1) {
        color = 'red'
      }
      return color
    }
  },
  created () {

  },
  mounted () {
    this.getGanguoSprayHis()
  }
}
</script>

<style scoped>

</style>
